
class Node {
    public int data;
    public Node next;//后继
    public Node prev;//前驱

    public Node(int data) {//构造方法
        this.data = data;
    }
}

public class MyNewLinkedList {
    public Node head;//标志双向链表的头
    public Node tail;//标志双向链表的尾

    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        if (this.tail == null && this.head == null) {
            this.head = node;
        } else {
            node.next = this.head;
            this.head.prev = node;
            this.head = node;
        }
    }

    //打印链表
    public void display() {
        Node cur = this.head;
        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //尾插法
    public void addLast(int data) {
        Node node = new Node(data);
        if (this.head == null && this.tail == null) {
            this.head = node;
            this.tail = node;
        } else {
            this.tail.next = node;
            node.prev = this.tail;
            this.tail = node;
        }
    }

    //查找是否包含关键字key
    public boolean contains(int key) {
        Node cur = this.head;
        while (cur != null) {
            if (cur.data == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //链表长度
    public int size() {
        int count = 0;
        Node cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }


    private void checkIndex(int index) {//检验合法性
        if (index < 0 || index > size()) {
            throw new RuntimeException("Index不合法！");
        }
    }

    private Node searchIndex(int index) {//找到下标号对应的节点
        Node cur = this.head;
        while (index > 0) {
            index--;
            cur = cur.next;
        }
        return cur;
    }

    //任意位置插入，第一个数据节点为0号下标
    public void addIndex(int index, int data) {
        if (index == 0) {
            addFirst(data);
        }
        if (index == size()) {
            addLast(data);
        }
        Node node = new Node(data);
        checkIndex(index);
        Node cur = searchIndex(index);
        cur.prev.next = node;
        cur.prev = node;
        node.prev = cur.prev;
        node.next = cur;
    }

    //删除第一次出现的关键字Key
    public void remove(int key) {
        Node cur = this.head;
        while (cur != null) {
            if (cur.data == key) {
                if (cur == this.head) {//要删除的是头结点
                    this.head = this.head.next;
                    this.head.prev = null;
                }
                cur.prev.next = cur.next;
                if (cur.next != null) {
                    cur.next.prev = cur.prev;
                } else {//要删除的是尾结点，移动tail即可
                    this.tail = cur.prev;
                }
                break;
            }
            cur = cur.next;
        }
    }

    //删除全部的关键字Key
    public void removeAllKey(int key) {
        Node cur = this.head.next;
        while (cur != null) {
            if (cur.data == key) {
                if (cur == this.head) {//要删除的是头结点
                    this.head = this.head.next;
                    if (this.head != null) {
                        this.head.prev = null;
                    }
                    cur.prev.next = cur.next;
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {//要删除的是尾结点，移动tail即可
                        this.tail = cur.prev;
                    }
                }
                cur = cur.next;
            }
        }
    }


    //删除链表,和单向链表不同，这里需要一个一个释放
    public void clear() {
        while (this.head != null) {
            Node cur = this.head.next;
            this.head.prev = null;
            this.head.next = null;
            this.head = cur;
        }
        this.tail = null;
    }
}

